package cn.my_server.z_transSQL;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

public class TransSQL extends JFrame implements ActionListener, KeyListener, MouseListener {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private JButton clean, execut;
    private JTextArea sqlField, parField, resField;

    /**
     * 构造函数
     *
     * @param string
     */
    public TransSQL(String string) {
        super(string);
    }

    /**
     * 方法体
     */
    public void run() {


        /**
         * 边框颜色
         */
        Color lineColor = new Color(127, 157, 185);

        /* 定义SQL语句输入框控件 */
        //JTextArea输入框控件
        sqlField = new JTextArea(20, 28);
        //设置输入框边框样式
        sqlField.setBorder(new LineBorder(lineColor, 1, false));
        //设置文本自动换行
        sqlField.setLineWrap(true);
        //初始化文本
        sqlField.setText("示例: select * from tabName where a=? and b=? and c=? and d=?");

        //JScrollPane定义带滚动条的控件,用来包含sqlField
        JScrollPane sqlScrollPane = new JScrollPane(sqlField);

        //JTextArea输入框控件
        parField = new JTextArea(20, 28);
        //设置输入框边框样式
        parField.setBorder(new LineBorder(lineColor, 1, false));
        //设置文本自动换行
        parField.setLineWrap(true);
        //初始化文本
        parField.setText("示例: a, b, c, d 或者  [a, b, c, d] 或者  00000(String), 00001(String)");
        //JScrollPane定义带滚动条的控件,用来包含sqlField
        JScrollPane parScrollPane = new JScrollPane(parField);

        //JTextArea输入框控件
        resField = new JTextArea(10, 60);
        //设置输入框边框样式
        resField.setBorder(new LineBorder(lineColor, 1, false));
        //设置文本自动换行
        resField.setLineWrap(true);
        //初始化文本
        resField.setText("匹配结果");
        //JScrollPane定义带滚动条的控件,用来包含sqlField
        JScrollPane resScrollPane = new JScrollPane(resField);

        /*定义一个sql块控件区,用来包含sql输入块*/
        //定义一个sql块控件区
        JPanel sql = new JPanel();
        //添加sql标签
        sql.add(new JLabel("SQL"));
        //将定义好的sql输入框控件包含进sql控件区
        sql.add(sqlScrollPane);

        /*定义一个parameter块控件区,用来包含parameter输入块*/
        //定义一个parameter块控件区
        JPanel parameter = new JPanel();
        //添加parameter标签
        parameter.add(new JLabel("参数"));
        //将定义好的parameter输入框控件包含进sql控件区
        parameter.add(parScrollPane);

        /*定义一个result块控件区,用来包含转换结果输入块*/
        //定义一个result块控件区
        JPanel result = new JPanel();
        //添加result标签
        result.add(new JLabel("结果"));
        //将定义好的result输入框控件包含进sql控件区
        result.add(resScrollPane);

        /*
         * 定义按钮,清除,执行
         * 并绑定事件
         */
        clean = new JButton("清除");
        clean.addActionListener(this);

        execut = new JButton("执行");
        execut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                /**
                 * 实际转换操作实现类.
                 */
                SQLFunction sqlFunc = new SQLFunction();
                //将转换结果置入result文本框
                resField.setText(sqlFunc.dealSQLwithPara(sqlField.getText(), parField.getText()));

            }
        });


        /*
         * 将sql和parameter控件区放在同一个区
         */
        JPanel mainJPanel = new JPanel();
        mainJPanel.add(sql);
        mainJPanel.add(parameter);

        /**
         * 结果输出单独一个区
         */
        JPanel resJPanel = new JPanel();
        resJPanel.add(result);
        resJPanel.setBounds(5, 5, 220, 500);

        /**
         * 将按钮置入一个控件区
         */
        JPanel menu = new JPanel();
        menu.add(clean);
        menu.add(execut);

        JPanel outerJPanel = new JPanel();
        outerJPanel.add(mainJPanel);
        outerJPanel.add(resJPanel);
        outerJPanel.add(menu);

        add(outerJPanel);
        setBounds(160, 60, 800, 700);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    /**
     * 初始化方法
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        sqlField.setText("");
        parField.setText("");
        resField.setText("");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}
